{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 04\n",
    "\n",
    "# 矩阵逐项积\n",
    "Book_4《矩阵力量》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb498579-9952-45fc-93e8-50479104f8f2",
   "metadata": {},
   "source": [
    "\n",
    "该代码定义了两个 $2 \\times 2$ 矩阵 $A$ 和 $B$，并计算它们的 Hadamard 积（逐元素乘积）。矩阵 $A$ 和 $B$ 分别为：\n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 5 & 6 \\\\ 7 & 8 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "Hadamard 积（逐元素乘积）的结果为每个对应元素相乘，计算公式为：\n",
    "\n",
    "$$\n",
    "A \\odot B = \\begin{bmatrix} 1 \\cdot 5 & 2 \\cdot 6 \\\\ 3 \\cdot 7 & 4 \\cdot 8 \\end{bmatrix} = \\begin{bmatrix} 5 & 12 \\\\ 21 & 32 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "代码使用 `np.multiply` 函数和 `*` 操作符两种方式来计算逐元素乘积，二者等效。该操作用于生成对应元素相乘的矩阵。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46ade452-7ec0-45e9-959c-1276be6d4724",
   "metadata": {},
   "source": [
    "## 导入所需库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c90949ae-66bd-4373-ac80-42341574da7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入NumPy库，用于数值计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b8e04f7-6950-42d1-9981-81ee65208cce",
   "metadata": {},
   "source": [
    "## 定义矩阵A和B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bab49ebe-1142-4a6b-9662-ccc569aaf590",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 2],  # 定义矩阵A\n",
    "              [3, 4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c63f1c20-b2b9-4688-ab7d-800ed8510add",
   "metadata": {},
   "outputs": [],
   "source": [
    "B = np.array([[5, 6],  # 定义矩阵B\n",
    "              [7, 8]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "861a16bd-2946-438e-8aed-1b974e7d99d6",
   "metadata": {},
   "source": [
    "## 计算Hadamard积（逐元素乘积）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f0947846-bc63-482d-a5ac-a6e891794832",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5, 12],\n",
       "       [21, 32]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_times_B_piecewise = np.multiply(A, B)  # 使用np.multiply计算A和B的逐元素乘积\n",
    "A_times_B_piecewise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7e81c46d-d82b-45fa-b22b-5f99b812f2e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5, 12],\n",
       "       [21, 32]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_times_B_piecewise_V2 = A * B  # 使用*操作符计算A和B的逐元素乘积\n",
    "A_times_B_piecewise_V2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
